package com.gsafety.wellsfort.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author tangjiawei
 * @date 2019/4/10
 */
public class StringHelper {
    /**
     * 方法描述:随机生成指定位数的由字母和数字组成的字符串
     *
     * @param length 补零后的长度
     * @return 字符串
     */
    public static String generateRandomCode(int length) {
        StringBuffer sb = new StringBuffer();
        final String baseData = "123456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(baseData.charAt(random.nextInt(baseData.length())));
        }
        return sb.toString();
    }

    /**
     * @param length 表示生成字符串的长度
     * @return
     */
    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成全数字类型指定长度的字符串
     *
     * @param length
     * @return
     */
    public static String getRandomDigitString(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 将[12,23] 格式规格转换成 Array
     *
     * @param str
     * @return
     */
    public static int[] convertStringTOArray(String str) {
        String[] array = null;
        int[] newArray = null;
        if (str.length() > 2) {
            String newString = str.substring(1, str.length() - 1);
            array = newString.split(",");
        }
        if (array != null) {
            newArray = new int[array.length];
            for (int i = 0; i < array.length; i++) {
                newArray[i] = Integer.parseInt(array[i]);
            }
        }
        return newArray;
    }

    public static String convertStringArrayToJson(String[] specValue) {
        JSONArray specArray = new JSONArray();
        for (int i = 0; i < specValue.length; i++) {
            specArray.add(specValue[i]);
        }
        return specArray.toString();
    }

    /**
     * 将[12,23] 格式规格转换成 List
     *
     * @param jsonArray
     * @return
     */
    public static <T> List<T> covertJsonArrayToList(String jsonArray, Class<T> cls) {
        if (StringUtils.isNotEmpty(jsonArray)) {
            return JSONObject.parseArray(jsonArray, cls);
        }
        return null;
    }

    /**
     * 字符串中数字用逗号隔开
     */
    public static List<Integer> convertStringToListInt(String str) {
        List<Integer> list = new ArrayList<>();
        String[] array = str.split(",");
        for (String s : array) {
            list.add(Integer.valueOf(s));
        }
        return list;
    }

    public static Integer[] convertStringToArrayInt(String str) {
        String[] strArray = str.split(",");
        Integer[] arr = new Integer[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            arr[i] = Integer.valueOf(strArray[i]);
        }
        return arr;
    }

}
